package top.kaoshanji.p1ch03a;

import top.kaoshanji.p1util.ClientAppConfig;
import top.kaoshanji.p1util.ConfigUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 代码清单-p1ch03a-01：客户端生成URL重定向到授权服务器
 * @author kaoshanji
 * ^_^
 * create time 2025/5/24 21:41
 */
@WebServlet(urlPatterns = {"/goOauthUrlCodeServlet"})
public class GoOauthUrlCodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("....GoOauthUrlCodeServlet...");

        ClientAppConfig clientAppConfig = ConfigUtil.getClientAppConfig();

        String toOauthUrl = clientAppConfig.getAuthorizationUri().concat("?")
                .concat("response_type=code&")
                .concat("redirect_uri=").concat(clientAppConfig.getRedirectUri()).concat("&")
                .concat("client_id=").concat(clientAppConfig.getClientId());

        System.out.println("...toOauthUrl..."+toOauthUrl);

        resp.sendRedirect(toOauthUrl); // 重定向一下

    }

}
